<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\Member;
use think\Controller;
use think\Request;

class User extends AdminBase
{
    private static $model = null;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        if (self::$model == null) {
            self::$model = new Member();
        }
    }

    /**
     * 显示用户
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function lists()
    {
        $get= $_GET;
        unset($get['/admin/user/lists']);
        $where=[];
        if (isset($get['nickname'])&&!empty($get['nickname'])){
            $where['nickname']=['like','%'.trim($get['nickname']).'%'];
        }
        if (isset($get['phone'])&&!empty($get['phone'])){
            $where['phone']=['like','%'.trim($get['phone']).'%'];
        }
        if (isset($get['time'])&&!empty($get['time'])){
            $time = trim($get['time']);
//            p($time);exit;
            $sartTime = strtotime(substr($time,0,19));
//            p($sartTime);
            $endTime = strtotime(substr($time,22,19));
            $where['register_time']=['between',[$sartTime,$endTime]];
        }
        $userData = self::$model->where($where)->paginate(15,false,['query'=>$get]);
        $this->assign('userData', $userData);
        return $this->fetch();
    }

    /**
     * 用户添加
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add()
    {
        if (\request()->isGet()) {
            return $this->fetch();
        }
        if (\request()->isAjax()) {
            $post = input('post.');
            $validate = validate('User');
            if (!$validate->scene('add')->check($post)) {
                return $this->resMessage(0, $validate->getError());
            }
            $userData = self::$model->where('phone', $post['phone'])->find();
            if ($userData != null) {
                return $this->resMessage(0, '手机号已经注册，请直接登录');
            }
            $nickData = self::$model->where('nickname', $post['nickname'])->find();
            if ($nickData != null) {
                return $this->resMessage(0, '昵称已经存在，请换一个');
            }
            $data = [
                'password' => get_md5_str($post['pass']),
                'nickname' => $post['nickname'],
                'sex' => $post['sex'],
                'phone' => $post['phone'],
                'lock_status' => 1,
                'register_way' => 1,
                'register_time' => time(),
                'login_time' => time(),
                'register_ip' => $_SERVER['SERVER_ADDR'],
            ];
            $res = self::$model->save($data);
            if ($res) {
                return $this->resMessage(1, '添加成功');
            } else {
                return $this->resMessage(0, '添加失败');
            }

        }

    }

    /**
     * 用户编辑
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        if (\request()->isGet()) {
            if (input('?param.id')) {
                $id = input('param.id');
                $userData = self::$model->where('id', $id)->find();
                $this->assign('userData', $userData);
                return $this->fetch();
            } else {
                abort(404);
            }

        }
        if (\request()->isAjax()) {
            $post = input('post.');
            $validate = validate('User');
            if (!$validate->scene('edit')->check($post)) {
                return $this->resMessage(0, $validate->getError());
            }
            if (isset($post['pass'])&&!empty($post['pass'])){
                if (strlen($post['pass'])>=6&&strlen($post['pass'])<=16){
                    if (isset($post['repass'])&&!empty($post['repass'])){
                        if ($post['pass']!=$post['repass']){
                            return $this->resMessage(0, '两次密码不一致');
                        }else{
                            $password = get_md5_str($post['pass']);
                        }
                    }else{
                        return $this->resMessage(0, '两次密码不一致');
                    }
                }else{
                    return $this->resMessage(0, '密码长度不能小于6位，大于16位');
                }

            }
            $id =$post['id'];
            $userData = self::$model->get($id);
            if ($userData==null){
                return $this->resMessage(0, '出错了');
            }
            $data = [
                'nickname' => $post['nickname'],
                'sex' => $post['sex'],
                'phone' => $post['phone'],
            ];
            if (isset($password)){
                $data['password']=$password;
            }
            $res = self::$model->save($data,['id'=>$id]);
            if ($res){
                return $this->resMessage(1, '修改成功');
            }else{
                return $this->resMessage(0, '修改失败');
            }


        }

    }

    /**
     * 用户删除
     * @return array
     * @throws \think\exception\DbException
     */
    public function del()
    {
        if (\request()->isAjax()){
            if (input('?post.id')){
                $id =input('post.id');
                $oldData = self::$model->get($id);
                if ($oldData!=null){
                    $res = self::$model->where('id',$id)->delete();
                    if ($res){
                        return $this->resMessage(1,'删除成功');
                    }else{
                        return $this->resMessage(0,'删除失败');
                    }
                }else{
                    return $this->resMessage(0,'数据不存在');
                }
            }else{
                abort(404);
            }
        }else{
            abort(404);
        }
    }

    /**
     * 更改用户状态
     * @return array
     * @throws \think\exception\DbException
     */
    public function upSta()
    {
        if (\request()->isAjax()){
            if (input('?post.id')&&input('?post.sta')){
                $id = input('post.id');
                $sta = input('post.sta');
                $oldData=  self::$model->get($id);
                if ($oldData==null){
                    return $this->resMessage(0,'数据不存在');
                }
                $res = self::$model->save(['lock_status'=>!$sta],['id'=>$id]);
                if ($res){
                    return $this->resMessage(1,'操作成功');
                }else{
                    return $this->resMessage(0,'操作失败');
                }
            }else{
                return $this->resMessage(0,'缺少参数');
            }
        }else{
            abort(404);
        }
    }
}
